/***
This do file plots the evolution of low-education job postings.
***/

*-------------------------------------------------------------------------------
* Setup 
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning) == 0) include "${root}/code/config_interactive.do"

* Set globals
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results"
cap mkdir "${root}/results/Jobs"

*-------------------------------------------------------------------------------
* Load Job Postings data
*-------------------------------------------------------------------------------
project, uses("${root}/data/web/data/Job Postings - National - Weekly.csv")
import delimited  "${root}/data/web/data/Job Postings - National - Weekly.csv", clear

* Plot range: Jan 1 2020 to Dec 31 2021
gen date = mdy(month, day_endofweek, year)
keep if inrange(date, mdy(1, 1, 2020), mdy(12, 31, 2021))

replace bg_posts_jzgrp12 = bg_posts_jzgrp12 * 100

tempfile job_national 
save `job_national'

*-------------------------------------------------------------------------------
* Re-weight by Q1 count of employees in Jan 2020 (confidential)
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/Employment/Employment Combined BY county income.dta")
use "${root}/data/dvc/Employment/Employment Combined BY county income.dta", clear 

keep if inrange(date, `=mdy(1, 1, 2020)', `=mdy(1, 31, 2020)')
keep if !missing(count_incq1)
gcollapse count_incq1, by(countyfips)

tempfile count_incq1_weights
save `count_incq1_weights'

*-------------------------------------------------------------------------------
* Collapse county-level jobposts to the national level
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/Burning Glass/Burning Glass - County - Weekly - No Impute.csv")
import delimited "${root}/data/dvc/Burning Glass/Burning Glass - County - Weekly - No Impute.csv", clear

merge m:1 countyfips using `count_incq1_weights', keep(match) nogen
gen date = mdy(month, day_endofweek, year)
keep if inrange(date, mdy(1, 1, 2020), mdy(12, 31, 2021))
gcollapse bg_posts_jzgrp12 [aweight = count_incq1], by(date)
replace bg_posts_jzgrp12 = bg_posts_jzgrp12 * 100

* Merge to national job posts dataset 
rename bg_posts_jzgrp12 bg_posts_jzgrp12_county 
merge 1:1 date using `job_national', assert(match) nogen

* Obtain x-axis labels
project, uses("${root}/code/ado/xlab_months.ado") raw
xlab_months, startmonth(Jan 2020) endmonth(Nov 2021) space(2)

*-------------------------------------------------------------------------------
* Plot the relative level of low-skill job postings over time, 
* separately for the national series and for the county-to-national series using
* Q1 employment as weights
*-------------------------------------------------------------------------------

line bg_posts_jzgrp12 date, color(oi2) || ///
line bg_posts_jzgrp12_county date, color(oi3) ///
	ytitle("Change in Job Postings (%)" "Relative to January 2020") ///
	xtitle("") ///
	ylab(60 "+60%" 40 "+40%" 20 "+20%" 0 "0%" -20 "-20%" -40 "-40%", nogrid) ///
	xlab( `r(xlab_months)', labsize(small)) ///
	yline(0, lcolor(gs8) lpattern(dash)) ///
	legend(label(1 "National Series") label(2 "National Series with Q1 employment weights")) ///
	xsize(10)

oi_graph_export "${root}/results/Jobs/Jobs - National Job Postings for Low-Education Workers over time", type(${fig_type})
